package com.estar.integaration.Util;

import com.alibaba.excel.util.DateUtils;
import com.estar.integaration.entity.paymentPlanEntity.PaymentPlanHistory;
import com.estar.integaration.entity.paymentPlanEntity.PurchaseOrderDetail;
import com.estar.integaration.entity.paymentPlanEntity.SupplierAccountPeriod;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Author: YJL
 * Date: 2022/2/17 18:28
 **/
public class DateParseUtil {

  /**
   * 日期加上账期得到最终的String的日期
   * @param curDate
   * @param realNeedAddDay
   * @return
   */
  public static String dateAddAccountPeriod(String curDate,String realNeedAddDay){
    Integer needAddDay = 0;
    if(realNeedAddDay == null)
      realNeedAddDay = "0";
    needAddDay = getRealNeedAddDay(realNeedAddDay);
    if(needAddDay == -1){
      //说明出现了特殊字符 暂时先用0
      needAddDay = 0;
    }
    if(curDate == null || curDate.equals("有到票台账但无日期"))
      return "有到票台账但无日期";
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-M-d");
    curDate = curDate.replaceAll("/","-");
    Date date = null;
    try {
      date = simpleDateFormat.parse(curDate);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    //日期加天数
    Calendar calendar = new GregorianCalendar();
    if(date == null)
      System.out.println("日期转换失败");
    calendar.setTime(date);
    calendar.add(calendar.DATE,needAddDay);
    date = calendar.getTime();
    //转换成字符串回去
    curDate = simpleDateFormat.format(date);
    curDate = curDate.replace("-","/");
    return curDate;
  }

  /**
   * 比较时间大小
   * @return
   * date1> date2 1
   * date1 = date2 0
   * date1 < date2 -1
   */
  public static Integer compareDate(String date1,String date2){
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/M/dd");
    try {
      Date d1 = sdf.parse(date1);
      Date d2 = sdf.parse(date2);
      if(d1.before(d2)){
        return -1;
      }else if(d2.before(d1)){
        return 1;
      }else {
        return 0;
      }
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return null;
  }


  /**
   * 根据时间排序（其他排序如根据id排序也类似）
   * @param list
   */
  public static void ListSort(List<PurchaseOrderDetail> list) {
    Collections.sort(list, new Comparator<PurchaseOrderDetail>() {
      @Override
      public int compare(PurchaseOrderDetail o1, PurchaseOrderDetail o2) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/M/dd");
        try {
          Date d1 = sdf.parse(o1.getExpectPayDate());
          Date d2 = sdf.parse(o2.getExpectPayDate());
          long time1 = d1.getTime();
          long time2 = d2.getTime();
          if(time1 == time2) {
            return 0;
          }else {
            if(d1.after(d2)){
              return 1;
            }else{
              return -1;
            }
          }
        } catch (ParseException e) {
          e.printStackTrace();
        }
        return 0;
      }
    });
  }

  /**
   * 排序PaymentHistory按照时间
   * @param list
   */
  public static void sortPaymentHistory(List<PaymentPlanHistory> list){
    Collections.sort(list, new Comparator<PaymentPlanHistory>() {
      @Override
      public int compare(PaymentPlanHistory o1, PaymentPlanHistory o2) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
          Date d1 = sdf.parse(o1.getUploadTime().toString());
          Date d2 = sdf.parse(o2.getUploadTime().toString());
          long time1 = d1.getTime();
          long time2 = d2.getTime();
          if(time1 == time2) {
            return 0;
          }else {
            if(d1.after(d2)){
              return -1;
            }else{
              return 1;
            }
          }
        } catch (ParseException e) {
          e.printStackTrace();
        }
        return 0;
      }
    });
  }


   static Integer getRealNeedAddDay(String realNeedAddDay){
    realNeedAddDay = realNeedAddDay.trim();
    Pattern pattern = Pattern.compile("[0-9]*");
    Matcher isNum = pattern.matcher(realNeedAddDay);
    if(!isNum.matches()){
      return -1;
    }
    return Integer.parseInt(realNeedAddDay);
  }

}
